﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Tnelab.Wpf
{
	public partial class WindowEx
	{
		public static readonly DependencyProperty IsMaximizeButtonProperty =
			DependencyProperty.RegisterAttached("IsMaximizeButton", typeof(bool), typeof(WindowEx), new(false, OnIsMaximizeButtonChanged));
		static void OnIsMaximizeButtonChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{

			var MaximizeButtonElement = obj as UIElement;
			if (e.NewValue != e.OldValue)
			{
				var value = (bool)e.NewValue;
				if (value)
				{
					MaximizeButtonElement.PreviewMouseLeftButtonUp += MaximizeButtonElement_MouseLeftButtonUp;
				}
				else
				{
					MaximizeButtonElement.PreviewMouseLeftButtonUp -= MaximizeButtonElement_MouseLeftButtonUp;
				}
			}
		}

		private static void MaximizeButtonElement_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			var window = Window.GetWindow(sender as DependencyObject);
			if (window.WindowState != WindowState.Maximized)
			{
				window.WindowState = WindowState.Maximized;
			}
            else if(window.WindowState==WindowState.Maximized)
            {
				window.WindowState = WindowState.Normal;
            }
		}

		public static bool GetIsMaximizeButton(UIElement dpo)
		{
			return (bool)dpo.GetValue(IsMaximizeButtonProperty);
		}

		public static void SetIsMaximizeButton(UIElement dpo, bool value)
		{
			dpo.SetValue(IsMaximizeButtonProperty, value);
		}
	}
}
